package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.DTO.ArticleDTO;
import com.itheima.DTO.YddArticlePageDTO;
import com.itheima.VO.ArticleVO;
import com.itheima.entity.YddArticle;
import com.itheima.json.PageBean;
import com.itheima.mapper.YddArticleMapper;
import com.itheima.service.YddArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class YddArticleServiceImpl implements YddArticleService {

    private final YddArticleMapper yddArticleMapper;

    @Override
    public PageBean page(YddArticlePageDTO yddArticleDTO) {
        PageHelper.startPage(yddArticleDTO.getPage(),yddArticleDTO.getLimit());
        List<YddArticle> yddArticleList = yddArticleMapper.page(yddArticleDTO);
        List<ArticleVO> articleVOList = transformToVO(yddArticleList);
        PageInfo pageInfo = PageInfo.of(articleVOList);
        PageBean pageBean = new PageBean();
        pageBean.setCount(articleVOList.size());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setCurrentPage(yddArticleDTO.getPage());
        pageBean.setPageSize(yddArticleDTO.getLimit());
        pageBean.setData(pageInfo.getList());
        return pageBean;
    }

    @Override
    public void insertArticleInfo(ArticleDTO articleDTO) {
        YddArticle yddArticle = articleDTOTransformYddArticle(articleDTO);
        yddArticle.setCreateDate(System.currentTimeMillis());
        yddArticle.setStatus(2);
        yddArticle.setUserId(136);
        yddArticleMapper.insertArticleInfo(yddArticle);
    }

    @Override
    public ArticleVO selectArticleInfoById(Integer id) {
        YddArticle yddArticle = yddArticleMapper.selectArticleInfoById(id);
        ArticleVO articleVO = yddArticletransformToVO(yddArticle);
        BeanUtils.copyProperties(yddArticle, articleVO);
        articleVO.setCreate_date(yddArticle.getCreateDate());
        articleVO.setUpdate_date(yddArticle.getUpdateDate());
        articleVO.setDefault_data(yddArticle.getDefaultData());
        return articleVO;
    }

    @Override
    public void deleteArticleById(Integer id) {
        yddArticleMapper.deleteArticleById(id);
    }

    @Override
    public void updateArticle(ArticleDTO articleDTO) {
        YddArticle yddArticle = articleDTOTransformYddArticle(articleDTO);
        yddArticleMapper.updateArticle(yddArticle);
    }

    private ArticleVO yddArticletransformToVO(YddArticle yddArticle) {
        ArticleVO articleVO = new ArticleVO();
        BeanUtils.copyProperties(yddArticle, articleVO);
        articleVO.setCreate_date(yddArticle.getCreateDate());
        articleVO.setUpdate_date(yddArticle.getUpdateDate());
        articleVO.setDefault_data(yddArticle.getDefaultData());
        return articleVO;
    }

    private List<ArticleVO> transformToVO(List<YddArticle> yddArticleList) {
        return yddArticleList.stream().map(yddArticle -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(yddArticle, articleVO);
            articleVO.setCreate_date(yddArticle.getCreateDate());
            articleVO.setUpdate_date(yddArticle.getUpdateDate());
            return articleVO;
        }).collect(Collectors.toList());
    }

    public YddArticle articleDTOTransformYddArticle(ArticleDTO articleDTO) {
        YddArticle yddArticle = new YddArticle();
        yddArticle.setId(articleDTO.getId());
        yddArticle.setTitle(articleDTO.getTitle());
        yddArticle.setAuthor(articleDTO.getAuthor());
        yddArticle.setKeywords(articleDTO.getKeywords());
        yddArticle.setDescription(articleDTO.getDescription());
        yddArticle.setContent(articleDTO.getContent());
        yddArticle.setPic(articleDTO.getPic());
        yddArticle.setClick(articleDTO.getClick());
        yddArticle.setIshot(articleDTO.getIshot());
        yddArticle.setIstop(articleDTO.getIstop());
        yddArticle.setCreateDate(articleDTO.getCreate_date());
        yddArticle.setUpdateDate(articleDTO.getUpdate_date());
        yddArticle.setCateid(articleDTO.getCateid());
        yddArticle.setTags(articleDTO.getTags());
        yddArticle.setStatus(articleDTO.getStatus());
        yddArticle.setLovenum(articleDTO.getLovenum());
        yddArticle.setUserId(articleDTO.getUser_id());
        yddArticle.setDefaultData(articleDTO.getDefault_data());
        yddArticle.setCatename(articleDTO.getCatename());
        return yddArticle;

    }
}
